<html>
<head><meta charset="utf-8"><title>Transmuting `-1isize` to a `Option&lt;fn&gt;`. · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20.60-1isize.60.20to.20a.20.60Option.3Cfn.3E.60.2E.html">Transmuting `-1isize` to a `Option&lt;fn&gt;`.</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="192988358"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20%60-1isize%60%20to%20a%20%60Option%3Cfn%3E%60./near/192988358" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20.60-1isize.60.20to.20a.20.60Option.3Cfn.3E.60.2E.html#192988358">(Apr 06 2020 at 01:49)</a>:</h4>
<p>SQLite's API requires passing a <code>-1isize</code> as a <code>Option&lt;unsafe extern "C" fn(a: *mut c_void)&gt;</code>: <a href="https://www.sqlite.org/c3ref/c_static.html" title="https://www.sqlite.org/c3ref/c_static.html">https://www.sqlite.org/c3ref/c_static.html</a> in some cases. Transmuting between these works, but it gives me the heebie jeebies and I suspect it's unsound...</p>
<p>I don't think rust code ever needs to call this, so I suspect we might be able to override bindgen and just use a repr(transparent) wrapper (I guess in theory a platform exists where isize and function pointers have a different ABI, but... realistically...)</p>



<a name="192991869"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20%60-1isize%60%20to%20a%20%60Option%3Cfn%3E%60./near/192991869" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20.60-1isize.60.20to.20a.20.60Option.3Cfn.3E.60.2E.html#192991869">(Apr 06 2020 at 03:18)</a>:</h4>
<p>You shouldn't need to transmute; you should be able to convert to usize and then to a fn type like any other pointer.</p>



<a name="192991874"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20%60-1isize%60%20to%20a%20%60Option%3Cfn%3E%60./near/192991874" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20.60-1isize.60.20to.20a.20.60Option.3Cfn.3E.60.2E.html#192991874">(Apr 06 2020 at 03:18)</a>:</h4>
<p>But see <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/72" title="https://github.com/rust-lang/unsafe-code-guidelines/issues/72">https://github.com/rust-lang/unsafe-code-guidelines/issues/72</a></p>



<a name="192991888"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20%60-1isize%60%20to%20a%20%60Option%3Cfn%3E%60./near/192991888" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20.60-1isize.60.20to.20a.20.60Option.3Cfn.3E.60.2E.html#192991888">(Apr 06 2020 at 03:19)</a>:</h4>
<p>"With the proposed wording for a future RFC, the only value you can't assign to a function pointer is 0, all other values are ok." ... but this hasn't actually been accepted yet</p>



<a name="192997494"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20%60-1isize%60%20to%20a%20%60Option%3Cfn%3E%60./near/192997494" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20.60-1isize.60.20to.20a.20.60Option.3Cfn.3E.60.2E.html#192997494">(Apr 06 2020 at 05:44)</a>:</h4>
<p><span class="user-mention" data-user-id="198590">@comex</span> hm? I'm not sure I follow what you mean by "and then to a fn type like any other pointer" -- I can't do <code>some_usize as unsafe extern "C" fn(a: *mut c_void)</code>.</p>



<a name="193001920"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20%60-1isize%60%20to%20a%20%60Option%3Cfn%3E%60./near/193001920" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20.60-1isize.60.20to.20a.20.60Option.3Cfn.3E.60.2E.html#193001920">(Apr 06 2020 at 07:08)</a>:</h4>
<p><span class="user-mention" data-user-id="198590">@comex</span> <code>fn</code> ptrs in Rust are safe to call and thus not safe to cast from. there are no raw fn ptrs.</p>



<a name="193001942"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20%60-1isize%60%20to%20a%20%60Option%3Cfn%3E%60./near/193001942" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20.60-1isize.60.20to.20a.20.60Option.3Cfn.3E.60.2E.html#193001942">(Apr 06 2020 at 07:08)</a>:</h4>
<p><span class="user-mention" data-user-id="209168">@Thom Chiovoloni</span> ah, you basically want <a href="https://github.com/rust-lang/unsafe-code-guidelines/pull/197" title="https://github.com/rust-lang/unsafe-code-guidelines/pull/197">https://github.com/rust-lang/unsafe-code-guidelines/pull/197</a> I think</p>



<a name="193002118"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20%60-1isize%60%20to%20a%20%60Option%3Cfn%3E%60./near/193002118" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20.60-1isize.60.20to.20a.20.60Option.3Cfn.3E.60.2E.html#193002118">(Apr 06 2020 at 07:10)</a>:</h4>
<p>Hmm, so AFAICT it's not strictly well defined, but there are no plans to make it UB (just the opposite) so I... probably am just not going to worry about it too much, since I don't have a lot of choice wrt using this API.</p>



<a name="193002872"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20%60-1isize%60%20to%20a%20%60Option%3Cfn%3E%60./near/193002872" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20.60-1isize.60.20to.20a.20.60Option.3Cfn.3E.60.2E.html#193002872">(Apr 06 2020 at 07:19)</a>:</h4>
<p>so right now it is well-defined if and only if fn ptrs have no alignment restrictions on your platform</p>



<a name="193002880"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20%60-1isize%60%20to%20a%20%60Option%3Cfn%3E%60./near/193002880" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20.60-1isize.60.20to.20a.20.60Option.3Cfn.3E.60.2E.html#193002880">(Apr 06 2020 at 07:19)</a>:</h4>
<p>see <a href="https://doc.rust-lang.org/stable/reference/behavior-considered-undefined.html" title="https://doc.rust-lang.org/stable/reference/behavior-considered-undefined.html">https://doc.rust-lang.org/stable/reference/behavior-considered-undefined.html</a></p>



<a name="193002908"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20%60-1isize%60%20to%20a%20%60Option%3Cfn%3E%60./near/193002908" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20.60-1isize.60.20to.20a.20.60Option.3Cfn.3E.60.2E.html#193002908">(Apr 06 2020 at 07:19)</a>:</h4>
<p>oh lol that actually doesnt even mention fn ptr alignment and just makes it well-defined</p>



<a name="193002962"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20%60-1isize%60%20to%20a%20%60Option%3Cfn%3E%60./near/193002962" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20.60-1isize.60.20to.20a.20.60Option.3Cfn.3E.60.2E.html#193002962">(Apr 06 2020 at 07:20)</a>:</h4>
<p>which I think is because all platforms we currently support do not make alignment requirements for fn ptrs</p>



<a name="193002969"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20%60-1isize%60%20to%20a%20%60Option%3Cfn%3E%60./near/193002969" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20.60-1isize.60.20to.20a.20.60Option.3Cfn.3E.60.2E.html#193002969">(Apr 06 2020 at 07:20)</a>:</h4>
<p>but we have no RFC that this cannot change in the future</p>



<a name="193012384"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20%60-1isize%60%20to%20a%20%60Option%3Cfn%3E%60./near/193012384" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20.60-1isize.60.20to.20a.20.60Option.3Cfn.3E.60.2E.html#193012384">(Apr 06 2020 at 08:53)</a>:</h4>
<p>if function pointers have an alignment restriction, the original C API doesn't work either :)</p>



<a name="193012497"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20%60-1isize%60%20to%20a%20%60Option%3Cfn%3E%60./near/193012497" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20.60-1isize.60.20to.20a.20.60Option.3Cfn.3E.60.2E.html#193012497">(Apr 06 2020 at 08:54)</a>:</h4>
<p>or rather, is undefined behavior in C</p>



<a name="193012522"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20%60-1isize%60%20to%20a%20%60Option%3Cfn%3E%60./near/193012522" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20.60-1isize.60.20to.20a.20.60Option.3Cfn.3E.60.2E.html#193012522">(Apr 06 2020 at 08:54)</a>:</h4>
<p>(it might still <em>work</em>)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>